<?php namespace Admin\Controller;
/*
 * 文章管理控制器
 * */
 	class ArticleController extends CommonController
 	{
 		protected $db;
 		/*
		 * 构造方法
		 * */
		public function __init()
		{
			$this->db = new \Common\Model\Article;
		}
 		/*
		 * 首页
		 * */
		public function index()
		{
			/*$articleData = $this->db->orderBy("aid","desc")->get();
			//p($articleData);die;
			foreach($articleData as $k=>$v){
				$cname = Db::table("category")->where("cid",$articleData[$k]["category_cid"])->pluck("cname");
				//p($cname) ;
				//追加数据
				$articleData[$k]["cname"] = $cname;
			}*/
			//分页
			//1.获得总记录的条数
			$count = $this->db->where("is_recycle",0)->count();
			//p($count);die;
			//2。每页的条数
			$page = Page::row(10)->make($count);
			//p($page);die;
			$articleData = $this->db
						->join('category','article.category_cid','=','category.cid')
						->where("is_recycle",0)
						->field("aid","title","cname")
						->limit(Page::limit())
						->get();
			//p($articleData);die;
			View::with("page",$page);
			View::with("articleData",$articleData);
			View::make("article");
		}
		
		/*
		 * 添加文章
		 * */
		public function addArticle()
		{
			if(IS_POST){
				if(!$this->db->add_Article()){
					View::error($this->db->getError());
				}else{
					View::success("添加成功",U("Article/index"));
				}
			}
			//获得所有分类以及标签
			$tagData = Db::table("tag")->get();
			//p($TagAll);die;
			View::with('tagData',$tagData);
			$TnameAll = Db::table('category')->get();
			//p($TnameAll);die;
			$TnameAll = Data::tree($TnameAll,"cname");
			//p($TnameAll);die;
			View::with("TnameAll",$TnameAll);
			//载入页面
			View::make();
		}
		/*
		 * 编辑
		 * */
		public function resetArticle()
		{
			//获得get参数
			$aid = Q("get.aid",0,"intval");
			if(IS_POST){
				if(!$this->db->edit($aid)){
					View::error($this->db->getEror());
				}else{
					View::success("编辑成功！",U("index"));
				}
			}
			//将原有信息返回页面
			$olddata = $this->db->where("aid",$aid)->first();
			//p($olddata);die;
			View::with("olddata",$olddata);
			$content = Db::table("article_data")->where("article_aid",$aid)->field("des","content")->first();
			//p($content);die;
			View::with("content",$content);
			//获得所有标签
			$tagData = Db::table("tag")->get();
			$Tid = Db::table("article_tag")->where("article_aid",$aid)->lists("tag_tid");
			//p($Tid);die;
			//p($TagAll);die;
			View::with('Tid',$Tid);
			View::with('tagData',$tagData);
			//获得所有类别
			$TnameAll = Db::table('category')->get();
			//p($TnameAll);die;
			//获得被编辑文章的category_cid，用于判断默认值
			$TnameAll = Data::tree($TnameAll,"cname");
			$Cid = $this->db->where("aid",$aid)->pluck("category_cid");
			//p($TnameAll);die;
			View::with("Cid",$Cid);
			View::with("TnameAll",$TnameAll);
		 	View::make();
		}
		/*
		 * 删除
		 * */ 
		public function del()
		{
			if(!$this->db->del()){
				View::error("删除失败");
			}else{
				View::success("删除成功",U("Article/index"));
			}
		}
		/*
		 * 回收站首页
		 * */
		public function ui_general()
		{
			//分页
			$count = $this->db->where("is_recycle",1)->count();
			//page
			$page = Page::row(10)->make($count);
			$data = $this->db
				  ->join("category","article.category_cid","=","category.cid")
				  ->where('is_recycle',1)
				  ->field("aid","title","cname")
				  ->limit(Page::limit())
				  ->get();
			View::with("page",$page);
			View::with("data",$data);
			View::make();
		}
		/*
		 * 回收站还原
		 * */
		public function reduction()
		{
			//获得aid
			$aid = Q("get.aid",0,"intval");
			
			if(!$this->db->reduction($aid)){
				View::error($this->db->getError());
			}else{
				View::success("成功",U('ui_general'));
			}
		}
		/*
		 * 回收站删除
		 * */
		public function Remove()
		{
			$aid = Q("get.aid",0,"intval");
			if(!$this->db->Remove($aid)){
				View::error("删除失败");
			}else{
				View::success("删除成功",U("Article/ui_general"));
			}
		}
 	}
